import { createLogger, format, transports } from 'winston';
import DailyRotateFile from 'winston-daily-rotate-file';

const customFormat = format.printf(({ level, message, timestamp, label, ...data}) => {
  let splat = (data[Symbol.for('splat')] || []) as any[];
  splat = splat.map(item => (typeof item === 'object' ? JSON.stringify(item) : item));
  const fullMessage = Array.isArray(splat) && splat.length ? `${message} ${splat.join(' ')}` : message;
  return `${timestamp} [${process.pid}] ${level.toUpperCase()} ${label} - ${fullMessage}`;
});

const logger = createLogger({
  level: 'info',
  format: format.combine(
    format.label({ label: 'isa-server' }),
    format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
    customFormat
  ),
  transports: [
    new transports.Console({
      format: format.combine(
        format.colorize(),
        format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
        customFormat
      )
    }),
    new DailyRotateFile({
      filename: 'logs/isa-server-%DATE%.log',
      datePattern: 'YYYY-MM-DD',
      zippedArchive: true,
      maxSize: '10m',
      maxFiles: '90d',
      format: format.combine(
        format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
        customFormat
      )
    })
  ]
});

export default logger; 